﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using VAdvantage.Apps;
using VAdvantage.Utility;
using VAdvantage.ProcessEngine;
namespace VAdvantage.KnowledgeBase
{
    public partial class ImageTextControl : UserControl
    {
        #region Declaration And Properties

        public event EventHandler<MouseButtonEventArgs> Click;
        
        /* primary key */
        private int id;
        /*Forign key */
        private int fkId;

        public static String ACTION_Workbench = "B";
        /** WorkFlow = F */
        public static String ACTION_WorkFlow = "F";
        /** Process = P */
        public static String ACTION_Process = "P";
        /** Report = R */
        public static String ACTION_Report = "R";
        /** Task = T */
        public static String ACTION_Task = "T";
        /** Window = W */
        public static String ACTION_Window = "W";
        /** Form = X */
        public static String ACTION_Form = "X";

        /* YouTube Image Link */
        private string imgYTUrl = "http://img.youtube.com/vi/{0}/0.jpg";

        /// <summary>
        /// Accessers primary key
        /// </summary>
        public int ID
        {
            private set;
            get;
        }

        public int FKID
        {
            private set;
            get;
        }

        /// <summary>
        /// Application Action
        /// </summary>
        public String Action
        {
            private set;
            get;
        }
        
        /// <summary>
        /// Video Id
        /// </summary>
        public string VID
        {
            get;
            set;
        }

        #endregion

        #region Constructor

        public ImageTextControl()
        {
            InitializeComponent();
            border.Visibility = System.Windows.Visibility.Collapsed;
        }

        public ImageTextControl(ImageSource source, String desc)
            : this()
        {
            txtDesc.Text = desc;
            img.Source = source;
        }

        public ImageTextControl(ImageSource source, String desc, int Id)
            : this()
        {
            txtHelp.Text = desc;
            img.Source = source;
            ID = Id;
            img.Cursor = Cursors.Hand;
        }

        public ImageTextControl(ImageSource source, string desc, string help, int id, string action, int fkId)
        {
            // TODO: Complete member initialization
            txtDesc.Text = desc;
            txtHelp.Text = help;
            img.Source = source;
            ID = id;
        }

        #endregion

        #region Events

        private void Border_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (Click != null)
            {
                Click(this, e);
            }
        }

        private void LayoutRoot_MouseEnter(object sender, MouseEventArgs e)
        {
            border.Visibility = System.Windows.Visibility.Visible;
        }

        private void LayoutRoot_MouseLeave(object sender, MouseEventArgs e)
        {
            border.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void btnAction_Click(object sender, RoutedEventArgs e)
        {
            if (FKID < 1)
            {
                MessageBox.Show("KfId");
                return;
            }
            if (Action.Equals("W"))				//	Window
            {

                StartWindow(0, FKID);
            }
            else if (Action.Equals("P") || Action.Equals("R"))	//	Process & Report
            {
                StartProcess(FKID);
            }
            else if (Action.Equals("B"))		//	Workbench
            {
                StartWindow(FKID, 0);
            }
            //                   //else if (Action.Equals("F"))		//	WorkFlow
            //                   //{
            //                   //    if (_isMenu)
            //                   //        cmd = Utility.Util.GetValueOfInt(dr["AD_Workflow_ID"]);
            //                   //    else
            //                   //        cmd = Utility.Util.GetValueOfInt(dr["Workflow_ID"]);

                //                   //    if (_host != null)
            //                   //    {
            //                   //        _host.ShowBusyIndicator = false;
            //                   //    }
            //                   //    //if (_menu != null)
            //                   //    //{
            //                   //    //    _menu.startWorkFlow(cmd);
            //                   //    //}
            //                   //}
            //                   else if (Action.Equals("T"))		//	Task
            //                   {

                //                       StartTask(FKID);
            //                   }
            else if (Action.Equals("X"))		//	Form
            {
                StartForm(FKID);
            }
            //else
            //    log.Log(Level.SEVERE, "No valid Action in ID=" + _ID);
            //}	//	for all records
        }

        private void ScrollViewer_SizeChanged(object sender, SizeChangedEventArgs e)
        {

            //if(LayoutRoot.ActualHeight <1)
            //    return;
            //double newHeight = (LayoutRoot.ActualHeight/2);

            //if (newHeight < e.NewSize.Height)
            //{
            //    svText.Height = newHeight;
            //}
            //else
            //    svText.Height = Double.NaN;
        }

        private void txtHelp_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (LayoutRoot.ActualHeight < 1)
                return;
            double newHeight = (LayoutRoot.ActualHeight / 3); // one third

            if (newHeight < e.NewSize.Height)
            {
                svText.Height = newHeight;
            }
            else
                svText.Height = Double.NaN;
        }

        /// <summary>
        /// Show Youtube dialog
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void imgYT_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (string.IsNullOrEmpty(VID))
            {
                return;
            }
            Dialogs.YTDialog d = new Dialogs.YTDialog(VID);
            d.Show();
        }

        #endregion

       #region Function

        internal void SetParameter(TopicInfo tInfo)
        {
            //txtName.Text = tInfo.Name;
            txtDesc.Text = tInfo.Desc;
            txtHelp.Text = tInfo.Help;
            img.Source = tInfo.Icon;
            ID = tInfo.ID;
            FKID = tInfo.FKID;
            Action = tInfo.Action;
            VID = tInfo.VID;
            SetImageLink();
            txtHelp.TextAlignment = TextAlignment.Left;

            if (!string.IsNullOrEmpty(tInfo.VID))
            {
                img.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri(string.Format(imgYTUrl, tInfo.VID)));
                txtYT.Text = tInfo.VLength;
                stYT.Visibility = System.Windows.Visibility.Visible;
                imgYT.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                stYT.Visibility = System.Windows.Visibility.Collapsed;
                imgYT.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        private void SetImageLink()
        {
            //if (String.IsNullOrEmpty(Action))
            //{
            //    imgAction.Source = null;
            //    btnAction.Visibility = System.Windows.Visibility.Collapsed;

            //}
            //else
            //{
            //    ImageSource s = VAdvantage.Utility.Envs.LoadImageSource(GetImageName(Action));
            //    imgAction.Source = s;
            //    btnAction.Visibility = System.Windows.Visibility.Visible;
            //}
        }

        internal void Clear()
        {
            txtDesc.Text = "";
            txtHelp.Text = "";
            
            img.Source = null;
            ID = 0;
            FKID = 0;
            Action = "";
            imgAction.Source = null;
            btnAction.Visibility = System.Windows.Visibility.Collapsed;
            //txtYT.Visibility = System.Windows.Visibility.Collapsed;
            imgYT.Visibility = System.Windows.Visibility.Collapsed;
            stYT.Visibility = System.Windows.Visibility.Collapsed;

        }

        private string GetImageName(string initial)
        {

            switch (initial)
            {
                case "W":
                    return "mWindow.png";
                case "R":
                    return "mReport.png";
                case "P":
                    return "mProcess.png";
                case "F":
                    return "mWorkflow.png";
                //case FieldAccessException"
                //    return "mWorkbench.png";
                case "X":
                    return "mWindow.png";
                //case 7:
                //    return "mWindow.png";
                case "T":
                    return "mDocAction.png";
                //case 10:
                //    return "folder.png";
                default:
                    return "mWindow.png";
            }
        }

        private void StartWindow(int AD_Workbench_ID, int AD_Window_ID)
        {


            if (Utility.Envs.ShowWindow(AD_Window_ID))
            {
                //_host.ShowBusyIndicator = false;
                return;
            }

            //SwingUtilities.invokeLater(m_updatePB);
            AWindow frame = new AWindow();
            //frame.Explorer = _host.GetExplorer;
            // frame.SetBusyIndicator(_host);
            // frame.SetTitle(_name);

            bool OK = false;
            if (AD_Workbench_ID != 0)
                OK = frame.InitWorkbench(AD_Workbench_ID);
            else
                OK = frame.InitWindow(AD_Window_ID, null);	//	No Query Value
            if (!OK)
                return;

            //SwingUtilities.invokeLater(m_updatePB);			//	2

            //if (frame != null)
            //{
            //    //frame.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            //   // frame.Show();
            //}
            frame = null;
        }

        private void StartProcess(int AD_Process_ID)
        {
            VAdvantage.Framework.ProcessDialog pd = new VAdvantage.Framework.ProcessDialog(AD_Process_ID);
            //pd.Explorer = Envs.RegisterWindowForm _host.GetExplorer;
            Envs.RegisterWindowForm(pd);
            //_host.GetProcessDialog.Visibility = System.Windows.Visibility.Visible;
            // pd.Closed += new EventHandler(pd_Closed);
            pd.Show();
        }

        private void StartForm(int AD_Form_ID)
        {

            //System.Windows.MessageBox.Show("Form Pending");
            //if (_host != null)
            //{
            //    _host.ShowBusyIndicator = false;
            //}

            if (AD_Form_ID == 115) // Tree Maintainance
            {
                // TreeMaintenance tree = new TreeMaintenance();
                // tree.Show();
                return;
            }
            if (AD_Form_ID == 117) //WorkFlowActivity
            {
                Apps.WF.WFActivity frmWfActivity = new VAdvantage.Apps.WF.WFActivity();
                Utility.Envs.RegisterWindowForm(frmWfActivity);
                frmWfActivity.Show();
                return;
            }

            if (AD_Form_ID == 102) // Initial Tenant Setup
            {
                VAdvantage.Framework.VSetup setup = new VAdvantage.Framework.VSetup();
                Utility.Envs.RegisterWindowForm(setup);
                setup.Show();
                return;
            }

            FormFrame ff = new FormFrame();
            ff.OpenForm(AD_Form_ID);
            //ff.Closing += new EventHandler<System.ComponentModel.CancelEventArgs>(ff_Closing);
            // ff.ShowDialog();
        }

#endregion

    }
}

